import VueRouter from "vue-router"
import Vue from 'vue'



const router = new VueRouter({
    mode: 'history',
    routes:[

        //登录
        {
            path:'/login',
            component:()=>import('../components/login/LoginPage')
        },
        //注册
        {
            path:'/register',
            name:'register',
            component:()=>import('../components/register/RegisterPage')
        },
        //聊天屋
        {
            path:'/chat',
            name:'chat',
            component:()=>import('../components/chat/ChatPage')
        }
    ],
    keepAlive: false,

})
router.beforeEach((to,from,next) =>{

    console.log(from.path+"->"+to.path)

    let username=localStorage.getItem("username")
    let isAdmin=JSON.parse(localStorage.getItem("isAdmin"))



    if(username!=null){
        Vue.prototype.$http.get('/user/tokenExists')
            .then(response => {

                //1.用户第一次登录，username必须是为空，跳转到登陆页面即可
                //2.用户登录后，手动输入url跳到/login，需要拦截下来，此时username!=null,

                if(to.path=='/login'||to.path=='/register'||to.path=='/logintext'||to.path=='/findpassword'){//修改了
                    if(username!=null){
                        //next('/homepage')
                        if(isAdmin==true){
                            next('/admin/homepage')
                        }else {
                            next('/homepage')
                        }
                    }else{
                        next()
                    }
                    return
                }

                if(username!=null){
                    //正则匹配
                    const regex = /^\/admin(\/.*)?$/;
                    //不是管理员，如果要跳转到管理员界面，则拦截回去homepage
                    if(!isAdmin){
                        //如果要跳转的访问路径满足正则匹配，则就是要跳转到管理员界面，则拦截
                        if(regex.test(to.path)){
                            next('/homepage')
                            return
                        }
                        //继续拦截无效路径
                        const resolved = router.resolve(to.path)
                        const isRouteExist = resolved.route.matched.length > 0
                        if(isRouteExist){
                            next()
                        }else {
                            if(to.path=='/post'||to.path=='/post'){

                            }
                            next('/homepage')
                        }
                        return;
                    }

                    //如果是管理员，要跳转的界面为不是/admin/**路径，则跳转回去
                    if(!regex.test(to.path)){
                        next('/admin/homepage')//
                        return
                    }

                    //如果访问/admin的话直接跳转到/admin/homepage
                    if(to.path=='/admin' ||to.path=='/admin/'){
                        next('/admin/homepage')
                        return
                    }
                    //拦截无效路径
                    const resolved = router.resolve(to.path)
                    const isRouteExist = resolved.route.matched.length > 0
                    if(isRouteExist){
                        next()
                    }else {
                        next('/admin')
                    }

                }else {
                    if(to.path=='/login'||to.path=='/register'||to.path=='/logintext'||to.path=='/findpassword'){
                        next()
                    }else {
                        next('/login')
                    }
                }
                console.log(response)
            })
            .catch(error => {
                // if (error.response.status == 401) {
                //     //删除缓存
                //     localStorage.clear()
                //     //跳转登录界面
                //     next('/login')
                //     Vue.prototype.$message.warning("账号失效，请重新登录！")
                //
                // }
                console.log(error)
                return;
            });
    }else {
        if(to.path=='/login'||to.path=='/register'||to.path=='/logintext'||to.path=='/findpassword'){
            next()
        }else {
            next('/login')
        }
    }

});
export default router